package com.pqtest.api.controller;

import com.pqtest.api.annotation.ResponseFormat;
import com.pqtest.api.dto.SimulibraryDto;
import com.pqtest.api.dto.TestlibraryDto;
import com.pqtest.api.service.TestlibraryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
public class TestlibraryController extends BaseController{
    @Autowired
    TestlibraryService testlibraryService;
    @GetMapping("/simutest")
    @ResponseFormat(type = "withSingle")
    Object getSimuTest(){
        return testlibraryService.getSimuTest();
    }

    //报名模拟测验
    @PostMapping("/simutest/apply")
    @ResponseFormat(type = "withSingle")
    Object applySimuTest(@Valid TestlibraryDto testlibraryDto, BindingResult bindingResult, OAuth2Authentication oAuth2Authentication) throws Exception {
        if(bindingResult.hasErrors()){
            throw new Exception(bindingResult.getFieldError().getDefaultMessage());
        }
        String uid = this.getUid(oAuth2Authentication);
        return testlibraryService.applySimuTest(Integer.parseInt(uid),testlibraryDto);
    }
    //检查是否报过名
    @PostMapping("/simutest/ifapply")
    @ResponseFormat(type = "withSingle")
    Object ifUserApplySimuTest(@Valid TestlibraryDto testlibraryDto, BindingResult bindingResult, OAuth2Authentication oAuth2Authentication) throws Exception {
        if(bindingResult.hasErrors()){
            throw new Exception(bindingResult.getFieldError().getDefaultMessage());
        }
        String uid = this.getUid(oAuth2Authentication);
        return testlibraryService.getIfUserApply(Integer.parseInt(uid),testlibraryDto);
    }
}
